package com.qf.fmall2202.user.shiro;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){

        final DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();

        //1. 配置直接放行的请求 url
        chainDefinition.addPathDefinition("/register.html","anon");
        chainDefinition.addPathDefinition("/swagger-ui.html","anon");
        chainDefinition.addPathDefinition("/doc.html","anon");
        chainDefinition.addPathDefinition("/webjars/**","anon");
        chainDefinition.addPathDefinition("/swagger-resources/**","anon");
        chainDefinition.addPathDefinition("/v2/**","anon");


        //2. 需要登录后才可以访问的请求url
//        chainDefinition.addPathDefinition("/**","authc");

        return chainDefinition;

    }

    @Bean
    public Realm customRealm(){
        final CustomRealm customRealm = new CustomRealm();

        // 密码算法设置

        final HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();

        hashedCredentialsMatcher.setHashAlgorithmName(ShiroConstants.HASH_ALGORITHM);

        hashedCredentialsMatcher.setHashIterations(ShiroConstants.HASH_TIMES);

        customRealm.setCredentialsMatcher(hashedCredentialsMatcher);


        return customRealm;
    }


    /**
     * 该配置，用于解决引入了 aop starter依赖后， 加了 @RequiresPermissions 注解 的controller失效的问题
     *
     * 如果不加，会出现把controller对应的url当初静态资源处理的问题 ，404
     * @return
     */
//    @Bean
//    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
//        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
//        creator.setProxyTargetClass(true);
//        return creator;
//    }

}
